﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data;
using System.Data.Linq;
using VQWEB.common;

namespace VQWEB.administrator.sources
{
    public class AdvertisementData
    {
        public core_advertisement GetAdvertisementById(int Id) {
            using (CoreModelDataContext content = new CoreModelDataContext())
            {
                var query = content.core_advertisements.Where(c => c.advertisement_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new core_advertisement();
            }
        }
        public int InsertOrUpdateAdvertisement(core_advertisement obj) { 
            try
            {
                int Id = 0;
                using (CoreModelDataContext context = new CoreModelDataContext())
                {
                    if (obj.advertisement_id > 0)
                    {
                        core_advertisement flag = GetAdvertisementById(obj.advertisement_id);                        
                        if (flag != null)
                        {
                            context.core_advertisements.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.advertisement_id;
                        }
                    }
                    else
                    {
                        context.core_advertisements.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.advertisement_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        
        }
        public DataTable ViewAdvertisement() {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_advertisements
                            select new { 
                                advertisement_id = c.advertisement_id,
                                advertisement_img = c.advertisement_img,
                                advertisement_width = c.advertisement_width,
                                advertisement_height = c.advertisement_height,
                                advertisement_embed = c.advertisement_embed,
                                advertisement_active = c.advertisement_active
                            
                            };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        }
        public void DeleteAdvertisement(List<int> array_id) {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_advertisements
                            where array_id.Contains(c.advertisement_id)
                            select c;
                if (query.Any())
                {
                    context.core_advertisements.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        }
        public void LockUnLockAdvertisement(List<int> array_id) {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_advertisements
                            where array_id.Contains(c.advertisement_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.advertisement_active = (item.advertisement_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
    }
}